﻿using Helper;
using Helper.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace Starehe.ViewModels
{    
    public class ViewExamResultsVM: ViewModelBase
    {
        bool isInStudentMode;
        bool isInClassMode;
        ExamResultStudentDisplayModel studentResult;
        ExamResultClassDisplayModel classResult;
        int selectedClassID;
        int selectedStudentID;
        ExamModel selectedExam;
        ObservableCollection<ExamModel> allExams;
        bool canExec =false;
        public ViewExamResultsVM()
        {
            InitVars();
            CreateCommands();
        }
        
        protected override void CreateCommands()
        {
            DisplayResultsCommand = new RelayCommand(async o => 
            {
                if (isInStudentMode)
                {
                    StudentResult = new ExamResultStudentDisplayModel(await DataAccess.GetExamResultAync(selectedStudentID, selectedExam.ExamID));
                    StudentModel st = await DataAccess.GetStudentAsync(selectedStudentID);
                    studentResult.NameOfStudent = st.NameOfStudent;
                    studentResult.NameOfClass = (await DataAccess.GetClassAsync(st.ClassID)).NameOfClass;
                    studentResult.NameOfExam = selectedExam.NameOfExam;
                }
            }, o => CanDisplayResults());
            DisplayResultsCommand.CanExecuteChanged += (o, e) =>
            {
                    CanExecute = DisplayResultsCommand.CanExecute(null);                   
            };
        }

        private bool CanExecute
        {
            get { return canExec; }
            set
            {
                if (canExec!=value)
                {
                    canExec = value;
                    if (canExec)
                        DisplayResultsCommand.Execute(null);
                }
            }
        }

        private void RefreshView()
        {
            CanExecute = DisplayResultsCommand.CanExecute(null); 
            if (canExec)
                DisplayResultsCommand.Execute(null);
        }
                
        private bool CanDisplayResults()
        {
            if (isInStudentMode)
                return selectedExam != null && selectedExam.ExamID > 0 && selectedStudentID > 0 &&
                    DataAccess.StudentExists(selectedStudentID);

            if (isInClassMode)
                return selectedExam != null && selectedExam.ExamID > 0 && selectedClassID > 0;

            return false;
        }

        protected override void InitVars()
        {
            
            Title = "VIEW EXAM RESULTS";
            IsInStudentMode = true;
            SelectedStudentID = 0;

        }

        public ObservableCollection<ExamModel> AllExams
        {
            get { return allExams; }

            private set
            {
                if (value != allExams)
                {
                    allExams = value;
                    NotifyPropertyChanged("AllExams");
                }
            }
        }

        public ExamResultStudentDisplayModel StudentResult
        {
            get { return studentResult; }

            private set
            {
                if (value != studentResult)
                {
                    studentResult = value;
                    NotifyPropertyChanged("StudentResult");
                }
            }
        }

        public ExamResultClassDisplayModel ClassResult
        {
            get { return classResult; }

            private set
            {
                if (value != classResult)
                {
                    classResult = value;
                    NotifyPropertyChanged("ClassResult");
                }
            }
        }
                
        public bool IsInStudentMode
        {
            get { return isInStudentMode; }

            set
            {
                if (value != isInStudentMode)
                {
                    isInStudentMode = value;
                    NotifyPropertyChanged("IsInStudentMode");
                    if (isInStudentMode)
                        StudentResult = new ExamResultStudentDisplayModel();
                    SelectedStudentID = 0;
                    
                }
            }
        }

        public bool IsInClassMode
        {
            get { return isInClassMode; }

            set
            {
                if (value != isInClassMode)
                {
                    isInClassMode = value;
                    NotifyPropertyChanged("IsInClassMode");
                    SelectedClassID = 0;
                    if(isInClassMode)
                    ClassResult = new ExamResultClassDisplayModel();
                }
            }
        }

        public ExamModel SelectedExam
        {
            get { return selectedExam; }

            set
            {
                if (value != selectedExam)
                {
                    selectedExam = value;
                    StudentResult = new ExamResultStudentDisplayModel();
                    ClassResult = new ExamResultClassDisplayModel();
                    RefreshView();
                    NotifyPropertyChanged("SelectedExam");
                }
            }
        }

        public int SelectedStudentID
        {
            get { return selectedStudentID; }

            set
            {
                if (value != selectedStudentID)
                {
                    selectedStudentID = value;
                    SelectedExam = null;
                    StudentResult = new ExamResultStudentDisplayModel();
                    NotifyPropertyChanged("SelectedStudentID");
                    RefreshView();
                    if (selectedStudentID > 0)
                        RefreshAllExams();
                    else AllExams.Clear();
                }
            }
        }

        private async void RefreshAllExams()
        {
            if (isInStudentMode)
            {
                int classID = await DataAccess.GetClassIDFromStudentID(selectedStudentID);
                AllExams = await DataAccess.GetExamsByClass(classID);
                return;
            }
            if (isInClassMode)
            {
                AllExams = await DataAccess.GetExamsByClass(selectedClassID);
            }
            
        }

        public int SelectedClassID
        {
            get { return selectedClassID; }
            set
            {
                if (value != selectedClassID)
                {
                    selectedClassID = value;
                    SelectedExam = null;
                    ClassResult = new ExamResultClassDisplayModel();
                    NotifyPropertyChanged("SelectedClassID");
                    if (selectedClassID > 0)
                        RefreshAllExams();
                    else AllExams.Clear();
                }
            }
        }
        
        public override void Reset()
        {
            
        }

        private ICommand DisplayResultsCommand
        {
            get;
            set;
        }
    }
}
